//
//  HomeCollectionViewController.swift
//  PhotoBrowser
//
//  Created by 游强辉 on 16/4/27.
//  Copyright © 2016年 youqianghui. All rights reserved.
//

import UIKit

private let reuseIdentifier = "homeCell"

class HomeCollectionViewController: UICollectionViewController {

    //MARK:- 私有成员变量
    private lazy var pictureArray : [Picture] = [Picture]()
    
    private lazy var photoAnimator = PhotoAnimator()
    
    //MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData(0)
    }
    
    
}

//MARK:- 加载数据
extension HomeCollectionViewController {
    private func loadData(offset : Int) {
        NetWorkTools.shareNetWorkTools.loadHomeData(offset) { (result, error) -> () in
            //1.校验错误
            if error != nil {
                print(error)
                return
            }
            
            //2.获取结果
            guard let result = result else {
                print("获取结果不正确")
                return
            }
            
            //3.遍历结果
            for dict in result {
                let picture = Picture(dict: dict)
                self.pictureArray.append(picture)
            }
            
            //4.刷新表格
            self.collectionView?.reloadData()
        }
    }
}

//MARK: - 数据源代理方法
extension HomeCollectionViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.pictureArray.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //1.创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HomeViewCell
        
        //2.给cell设置数据
        cell.picture = pictureArray[indexPath.item]
        
        if indexPath.item == self.pictureArray.count - 1 {
            loadData(self.pictureArray.count)
        }
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        showPhotoBrowser(indexPath)
    }
    
}

//MARK: - 自定义控制器
extension HomeCollectionViewController {
    /// 弹出图片控制器
    func showPhotoBrowser(indexPath : NSIndexPath) {
        let photoVC = PhotoBrowserViewController()
        
        //设置属性
        photoVC.pictureArray = self.pictureArray
        photoVC.pictureIndexPath = indexPath
        
        photoVC.modalPresentationStyle = .Custom
        photoVC.transitioningDelegate = photoAnimator
        
        photoAnimator.presentDelegate = self
        photoAnimator.dismissDelegate = photoVC
        photoAnimator.indexPath = indexPath
        
        presentViewController(photoVC, animated: true, completion: nil)
    }
}

//MARK: 实现presentDelegate代理方法
extension HomeCollectionViewController : PresentDelegate {
    
    func homeRect(indexPath: NSIndexPath) -> CGRect {
        //1.取出cell
        let homeCell = (collectionView?.cellForItemAtIndexPath(indexPath))!
        return collectionView!.convertRect(homeCell.frame, toCoordinateSpace: UIApplication.sharedApplication().keyWindow!)
    }
    
    func photoBrowserRect(indexPath: NSIndexPath) -> CGRect {
        //1.取出cell
        let cell = (collectionView?.cellForItemAtIndexPath(indexPath))! as! HomeViewCell
        
        //2.取出图片
        let image = cell.imageView.image!
        
        //3.计算大图的frame
        return calculateFrame(image)
    }
    
    func imageView(IndexPath: NSIndexPath) -> UIImageView {
        
        //1.创建一个UIImageView的对象
        let imageView = UIImageView()
        
        //2.设置显示的图片
        //2.1取出cell
        let cell = (collectionView?.cellForItemAtIndexPath(IndexPath))! as! HomeViewCell
        
        //2.2取出图片
        let image = cell.imageView.image!
        
        //2.3设置图片
        imageView.image = image
        
        //3.设置imageView的属性
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }
}




